#include <bits/stdc++.h>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    bool hasPathSum(TreeNode *root, int targetSum)
    {
        if (root == nullptr)
        {
            return false;
        }

        queue<TreeNode *> que_node;
        queue<int> que_val;

        que_node.push(root);
        que_val.push(root->val);

        while (!que_node.empty())
        {
            TreeNode *cur = que_node.front();
            int temp = que_val.front();
            que_node.pop();
            que_val.pop();
            if (cur->left == nullptr && cur->right == nullptr)
            {
                if (temp == targetSum)
                {
                    return true;
                }

                continue;
            }

            if (cur->left != nullptr)
            {
                que_node.push(cur->left);
                que_val.push(cur->left->val + temp);
            }

            if (cur->right != nullptr)
            {
                que_node.push(cur->right);
                que_val.push(cur->right->val + temp);
            }
        }

        return false;
    }
};
